package com.rfsp.common.config;

import javax.naming.NamingException;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.jndi.JndiObjectFactoryBean;

@Configuration
@Profile(value = { "prod" })
public class DataSourceConfig {
	
	@Value("${spring.datasource.jndi-name}")
	private String jndiUrl;

	@Bean("dataSource")
	@Primary
	public DataSource dataSource() throws IllegalArgumentException, NamingException{
	    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
	    bean.setJndiName(jndiUrl);
	    bean.setProxyInterface(DataSource.class);
	    bean.setLookupOnStartup(false);
	    bean.afterPropertiesSet();
	    return (DataSource)bean.getObject();
	}
}
